package services

import (
	"github.com/mlogclub/simple/sqls"
	"github.com/mlogclub/simple/web/params"

	"bbs-go/internal/models"
	"bbs-go/internal/repositories"
)

var SystemNoticeService = newSystemNoticeService()

func newSystemNoticeService() *systemNoticeService {
	return &systemNoticeService{}
}

type systemNoticeService struct {
}

func (s *systemNoticeService) Get(id int64) *models.SystemNotice {
	return repositories.SystemNoticeRepository.Get(sqls.DB(), id)
}

func (s *systemNoticeService) Take(where ...interface{}) *models.SystemNotice {
	return repositories.SystemNoticeRepository.Take(sqls.DB(), where...)
}

func (s *systemNoticeService) Find(cnd *sqls.Cnd) []models.SystemNotice {
	return repositories.SystemNoticeRepository.Find(sqls.DB(), cnd)
}

func (s *systemNoticeService) FindPageByParams(params *params.QueryParams) (list []models.SystemNotice, paging *sqls.Paging) {
	return repositories.SystemNoticeRepository.FindPageByParams(sqls.DB(), params)
}

func (s *systemNoticeService) FindPageByCnd(cnd *sqls.Cnd) (list []models.SystemNotice, paging *sqls.Paging) {
	return repositories.SystemNoticeRepository.FindPageByCnd(sqls.DB(), cnd)
}

func (s *systemNoticeService) Create(t *models.SystemNotice) error {
	err := repositories.SystemNoticeRepository.Create(sqls.DB(), t)
	return err
}

func (s *systemNoticeService) Update(t *models.SystemNotice) error {
	err := repositories.SystemNoticeRepository.Update(sqls.DB(), t)
	return err
}

func (s *systemNoticeService) Updates(id int64, columns map[string]interface{}) error {
	err := repositories.SystemNoticeRepository.Updates(sqls.DB(), id, columns)
	return err
}

func (s *systemNoticeService) UpdateColumn(id int64, name string, value interface{}) error {
	err := repositories.SystemNoticeRepository.UpdateColumn(sqls.DB(), id, name, value)
	return err
}

func (s *systemNoticeService) FindShowIndexList() (list []models.SystemNotice, err error) {
	db := sqls.DB()
	err = db.Model(&models.SystemNotice{}).Where("status > ?", 2).Order("sort asc").Find(&list).Error
	return
}

func (s *systemNoticeService) GetById(id int64) *models.SystemNotice {
	systemNotice := models.SystemNotice{}
	db := sqls.DB()
	db.Model(&models.SystemNotice{}).Where("status >= ? and id = ?", 2, id).Order("sort asc").First(&systemNotice)
	return &systemNotice
}
